package com.usian.wemedia.controller;

import com.usian.apis.wemedia.WmNewsControllerApi;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.pojos.WmNewsVo;
import com.usian.model.media.pojos.WmUser;
import com.usian.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements WmNewsControllerApi {

    @Autowired //默认根据类型 ，要根据名称  @
    //@Qualifier("wmNewsService")
    private WmNewsService wmNewsService;

    @Value("${fileServerUrl}")
    private String fileServerUrl;

    @Override
    @RequestMapping("list")
    public ResponseResult findAll(@RequestBody WmNewsPageReqDto wmNewsPageReqDto) {
        return wmNewsService.findAll(wmNewsPageReqDto);
    }

    /**
     * 文章的保存。修改
     * @param wmNews
     * @return
     */
    @Override
    @PostMapping("/submit")
    public ResponseResult submitNews(@RequestBody WmNewsDto wmNews) {
        if(wmNews.getStatus()== WmNews.Status.SUBMIT.getCode()){
            //提交文章
            return wmNewsService.saveNews(wmNews, WmNews.Status.SUBMIT.getCode());
        }else{
            //保存草稿
            return wmNewsService.saveNews(wmNews, WmNews.Status.NORMAL.getCode());
        }
    }

    /**
     * 根据文章id获取文章详情
     * @param newId
     * @return
     */
    @Override
    @GetMapping("one/{newId}")
    public ResponseResult findById(@PathVariable("newId") Integer newId) {
        if(newId==null){
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmNews wmNews = wmNewsService.getById(newId);
        if(wmNews!=null){
            ResponseResult responseResult = ResponseResult.okResult(wmNews);
            responseResult.setHost(fileServerUrl);
            return responseResult;
        }
        return ResponseResult.okResult(null);
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @Override
    @GetMapping("/del_news/{id}")
    public ResponseResult delNews(@PathVariable Integer id) {
        return wmNewsService.delNews(id);
    }

    /**
     * 上下架
     * @param dto
     * @return
     */
    @PostMapping("/down_or_up")
    @Override
    public ResponseResult downOrUp(@RequestBody WmNewsDto dto) {
        return wmNewsService.downOrUp(dto);
    }

    /**
     * 根据文章id查询文章详情
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/findOne/{id}")
    public WmNews selectById(@PathVariable Integer id) {
        return wmNewsService.getById(id);
    }

    /**
     * 审核修改文章状态  id status
     * @param wmNews
     * @return
     */
    @PostMapping("/update")
    public ResponseResult updateWmNews(@RequestBody WmNews wmNews) {
        boolean b = wmNewsService.updateById(wmNews);
        if(b){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR); //保存失败
    }

    /**
     * 查询待发布列表 用于定时任务去发布
     *
     * @return
     */
    @Override
    @GetMapping("/findRelease")
    public List<Integer> findRelease() {
        return wmNewsService.findRelease();
    }

    /**
     * 分页查询人工待审核的列表
     *
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/findList")
    public PageResponseResult findList(@RequestBody NewsAuthDto dto) {

        return (PageResponseResult)wmNewsService.findListAndPage(dto);
    }

    /**
     * 根据文章id查询文章详情 带作者名字
     * @param id
     * @return
     */
    @Override
    @GetMapping("/find_news_vo/{id}")
    public WmNewsVo findWmNewsVo(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsVo(id);
    }


}
